home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Programming / imageio.lib / dev / examples / savedecoded / main.c next >
Encoding:
C/C++ Source or Header  |  2000-08-09  |  3.0 KB  |  125 lines

  1. /* This example use of imageio.library loads the image file specified
  2.         on the command line (for the example, make sure it is an encoded file,
  3.         uu or base64), and saves a decoded version to the file 'ram:test'.
  4. */
  5.  
  6. #include <stdio.h>
  7.  
  8. #include <dos/dos.h>
  9. #include <exec/memory.h>
  10. #include <exec/types.h>
  11.  
  12. #include <clib/dos_protos.h>
  13. #include <clib/exec_protos.h>
  14. #include <clib/intuition_protos.h>
  15.  
  16. #include <pragmas/dos_pragmas.h>
  17. #include <pragmas/exec_pragmas.h>
  18. #include <pragmas/intuition_pragmas.h>
  19.  
  20. #include <imageio/imageio.h>
  21. #include <imageio/imageio_protos.h>
  22. #include <imageio/imageio_pragmas.h>
  23.  
  24. /* Function prototypes */
  25. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata );
  26.  
  27. extern struct Library *DOSBase;
  28. struct Library *ImageIOBase, *IntuitionBase;
  29.  
  30. void main( int argc, char **argv )
  31. {
  32.     if ( argv[1] != NULL )
  33.     {
  34.         ImageIOBase = OpenLibrary( "imageio.library", 2 );
  35.         IntuitionBase = OpenLibrary( "intuition.library", NULL );
  36.         if ( IntuitionBase && ImageIOBase )
  37.         {
  38.             struct ImageHandle *ih;
  39.             ULONG err;
  40.             BPTR fp;
  41.  
  42.             fp = Open( argv[1], MODE_OLDFILE );
  43.             if ( fp != NULL )
  44.             {
  45.                 err = AllocImage( &ih,
  46.                     IMG_SrcFile, fp,
  47.                     TAG_DONE );
  48.                 if ( !err )
  49.                 {
  50.                     ULONG x, y, bpp, rs;
  51.                     UBYTE *buffer, cs, it;
  52.                     BOOL uu;
  53.                     char decodedname[50];
  54.  
  55.                     err = GetImageAttrs( ih,
  56.                         IMG_ImageType, &it,
  57.                         TAG_DONE );
  58.                     if ( !err )
  59.                     {
  60.                         printf("Image type=%d\n",it);
  61.                     }
  62.  
  63.                     err = GetImageAttrs( ih,
  64.                         IMG_Width, &x,
  65.                         IMG_Height,&y,
  66.                         IMG_BytesPerPixel, &bpp,
  67.                         IMG_ColourSpace, &cs,
  68.                         IMG_RowSize, &rs,
  69.                         IMG_IsDecoded, &uu,
  70.                         IMG_DecodedName, decodedname,
  71.                         TAG_DONE );
  72.                     if ( !err )
  73.                     {
  74.                         printf( "width=%ld, height=%ld\n", x, y );
  75.                         printf( "bytes per pixel=%ld, colourspace=%d\n", bpp, cs );
  76.                         printf( "row size=%ld\n", rs );
  77.                         printf( "image is decoded %s\n", uu == TRUE ? "yes" : "no" );
  78.                         printf( "decoded name=%s\n", decodedname );
  79.  
  80.                         err = ReadImage( ih,
  81.                             IMG_ImageBuffer, &buffer,
  82.                             IMG_ProgressHook, progressFunc,
  83.                             TAG_DONE );
  84.                         if ( !err )
  85.                         {
  86.                             err = WriteImage( ih,
  87.                                 IMG_WriteDecodedFile, "ram:test",
  88.                                 TAG_DONE );
  89.                             if ( err ) printf( "save err=%ld\n", err );
  90.                         }
  91.                         else printf( "read image error:%d\n", err );
  92.                     }
  93.                     else printf( "get image attrs error:%d\n", err );
  94.  
  95.                     FreeImage( ih );
  96.                 }
  97.                 else printf( "alloc image error:%d\n", err );
  98.  
  99.                 Close( fp );
  100.             }
  101.             else printf( "cant open file\n" );
  102.         }
  103.  
  104.         if ( ImageIOBase ) CloseLibrary( ImageIOBase );
  105.         if ( IntuitionBase ) CloseLibrary ( IntuitionBase );
  106.     }
  107.     else printf( "no file specified\n" );
  108. }
  109.  
  110. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata )
  111. {
  112.     static int prevpercent = 0;
  113.  
  114.     int percent = ( curr * 100 ) / lines;
  115.  
  116.     if ( prevpercent != percent )
  117.     {
  118.         if ( percent % 10 == 0 ) printf( "%d%%\n", percent );
  119.     }
  120.  
  121.     prevpercent = percent;
  122.  
  123.     return NULL;
  124. }
  125.